#TODO: 这题还有问题
class Solution:
    def getSkyline(self, buildings: list) -> list:
        if not buildings:
            return []
        buildings.sort(key=lambda lists: lists[2], reverse=True)
        res_list = [[buildings[0][0], buildings[0][2]], [buildings[0][1], 0]]
        for building in buildings[1:]:
            i = 0
            try:
                while res_list[i][0] < building[0]:
                    i += 1
            except:
                # 整体在右侧的情况
                res_list.extend([[building[0], building[2]], [building[1], 0]])
            else:
                if building[1] < res_list[i][0]:
                    # 在左侧没贴住的情况
                    res_list.insert(i, [building[1], 0])
                    res_list.insert(i, [building[0], building[2]])
                elif building[1] == res_list[i][0]:
                    # 左边贴住的情况
                    if building[2] == res_list[i][1]:
                        # 如果高度一样的情况
                        res_list[i][0] = building[0]
                    else:
                        # 高度不一样的情况
                        res_list.insert(i, [building[0], building[2]])
                else:
                    j = i
                    while res_list[j][1] != 0:
                        j += 1
                    if j == i:
                        # 右边贴住的情况
                        if res_list[i - 1][1] == building[2]:
                            # 高度一样的情况
                            res_list[i][0] = building[1]
                        else:
                            # 高度不一样的情况
                            res_list.insert(i, [res_list[i][0], building[2]])
                            res_list[i + 1][0] = building[1]
                    else:
                        if res_list[j][0] < building[1]:
                            res_list.insert(j, [res_list[j][0], building[2]])
                        res_list[j][0] = max(res_list[j][0], building[1])
                        if res_list[i][0] > building[0]:
                            res_list.insert(i, [building[0], building[2]])
        return res_list


a = Solution()
print(a.getSkyline([[2, 9, 10], [3, 7, 15], [5, 12, 12], [15, 20, 10], [19, 24, 8]]))
print(a.getSkyline([[1, 2, 2], [1, 2, 1], [1, 2, 3]]))
print(a.getSkyline([[0, 2, 3], [2, 5, 3]]))  # [[0,3],[5,0]]
print(a.getSkyline([[2, 9, 10], [9, 12, 15]]))
print(a.getSkyline([[2, 13, 10], [10, 17, 25], [12, 20, 14]]))
print(a.getSkyline([[0, 3, 3], [1, 5, 3], [2, 4, 3], [3, 7, 3]]))  # [[0,3],[7,0]]
print(a.getSkyline([[2, 9, 10], [3, 7, 15], [5, 12, 12], [15, 20, 10], [19, 24, 8]]))
